use std::any::Any;
use log::info;
use tokio::sync::mpsc;

#[tokio::test]
pub async fn mpsc_test() {

   let (sender, mut receive) = mpsc::channel(10);
   let sender2 = sender.clone();
   tokio::spawn(async move {
      for i in 0..10 {
         sender2.send(i).await.unwrap();
      }
   });

   drop(sender);

   while let Some(message) = receive.recv().await {
      info!("receive: {:?}", message);
   }
}

#[tokio::test]
pub async fn mpsc_test2() {

   let (sender, mut receive) = mpsc::channel::<Box<dyn Any + Send>>(10);
   let sender2 = sender.clone();
   tokio::spawn(async move {
      for i in 0..10 {
         sender2.send(Box::new(i)).await.unwrap();
         sender2.send(Box::new(i.to_string())).await.unwrap();
      }
   });

   drop(sender);

   while let Some(message) = receive.recv().await {
      if message.is::<i32>() {
         if let Ok(num) = message.downcast::<i32>() {
            info!("Received i32: {}", num);
         }
      } else if message.is::<String>() {
         if let Ok(s) = message.downcast::<String>() {
            info!("Received String: {}", s);
         }
      }
   }
}

#[test]
fn runable_test() {
   let inner = tokio::runtime::Builder::new_multi_thread()
       .enable_all()
       .build()
       .unwrap()
       .block_on(async {
          info!("Hello world");
       });
}